package YAVMM::CLI::Plugin::Host;
use Moose::Role;

use Dancer qw(:moose !options);
use MooseX::Method::Signatures;

use YAVMM::Error;
use YAVMM::Error::RPC;
use YAVMM::Error::Parameter;

use YAVMM::Host;

use Getopt::Long qw(GetOptionsFromArray :config auto_version auto_help bundling pass_through require_order);

method clusters {
    my $obj = $self->_json_rpc(path => '/json/clusters');

    print join(' ', @{ $obj }), "\n";
}

method hosts {

    my %local_opts = ();

    GetOptionsFromArray(
	$self->argv,
	\%local_opts,
	'cluster|c=s',
	'verbose|v',
    );

    my $res = $self->_json_rpc(
	path => '/json/hosts',
	args => {
	    cluster => $local_opts{cluster},
	}
    );

    # TODO: error check $res

    if ($local_opts{verbose}) {
	foreach my $host (map { YAVMM::Host->unpack($_) } @{ $res }) {
	    print "name: ", $host->name, ' (', $host->cluster, ')',"\n";
	    printf "  %10s : %d\n", 'CPUs', $host->num_cpus;
	    printf "  %10s : %.2f MB\n", 'RAM', $host->ram / 1024;
	}
    }
    else {
	print join ' ',
	    map { my $h = YAVMM::Host->unpack($_); $h->name }
		@{ $res };
	print "\n";
    }
}

method update_stats {
    my $res = $self->_json_rpc(
	path => '/json/update_stats',
    );

    #print $res->{status};
}

1;
